package com.gxa.controller.SystemController;


import com.gxa.entity.Admin;
import com.gxa.entity.Menu;
import com.gxa.entity.Role;
import com.gxa.service.system.RoleService;
import com.gxa.utils.R;
import com.gxa.vo.MenuVo;
import com.gxa.vo.RoleVo;
import com.gxa.vo.RoleVo1;
import com.gxa.vo.RoleVo2;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

@Api(tags = "角色管理模块")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ApiOperation(value="Role列表接口",notes="查询全部角色信息",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回所有的角色详细信息",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("role:sel")
    @GetMapping("/role/qureyAllRole")
    public R queryAllRole(){
        List<RoleVo> roleVos = roleService.queryAll();
        return R.ok().put("data",roleVos);
    }

    @ApiOperation(value="Role查询接口",notes="根据输入内容查询角色信息",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回所有的角色详细信息",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("role:sel")
    @PostMapping("/role/qureyRoles")
    public R queryRoles(@ApiParam(name="role",value = "角色名参数--这里输入要查询的角色名",required = true)@RequestBody RoleVo roleVo){
        System.out.println(roleVo.getName());
        List<RoleVo> roleVos = roleService.queryRoleByName("%"+roleVo.getName()+"%");
        return R.ok().put("data",roleVos);

    }


    @ApiOperation(value="进入Role增加接口",notes="点击增加角色进入接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回权限列表",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("role:sel")
    @GetMapping("/role/preAddRole")
    public R preAddRole(){
        List<MenuVo> menuVos = roleService.selAllMenu();
        return R.ok().put("data",menuVos);

    }

    @ApiOperation(value="Role增加接口",notes="增加角色接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回增加是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("role:add")
    @PostMapping("/role/addRole")
    public R addRole(@ApiParam(name="role",value = "角色参数--这里写输入的角色信息描述",required = true)@RequestBody RoleVo1 role){
        System.out.println(role);
        List<Integer> menus = role.getMenus();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        role.setTime(sdf.format(new Date()));
        Integer integer = roleService.addRole(role);
        for(Integer menuVo : menus){
            roleService.addRoleMenu(role.getId(),menuVo);
        }
        return R.ok();
    }

    @ApiOperation(value="进入编辑Role接口",notes="进入操作列编辑角色操作接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "根据角色id查询到的角色详细信息",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("role:sel")
    @PostMapping("/role/preEditRole")
    public R preEditRole(@ApiParam(name="role",value = "角色id",required = true)@RequestBody RoleVo role){
        System.out.println(role);
        RoleVo2 roleVo = roleService.queryRoleById(role.getId());
        List<MenuVo> menuVos = roleService.selAllMenu();
        return R.ok().put("data",roleVo).put("menuVos",menuVos);
    }

    @ApiOperation(value="编辑Role接口",notes="编辑角色操作接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回编辑是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("role:update")
    @PutMapping("/role/editRole")
    public R editRole(@ApiParam(name="role",value = "角色参数--这里写输入的角色信息描述",required = true)@RequestBody RoleVo1 role){
        System.out.println(role);
        roleService.updateRole(role);
        return R.ok();
    }

    @ApiOperation(value="删除Role接口",notes="删除角色操作接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回删除是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("role:del")
    @DeleteMapping("/role/delRole")
    public R delRole(@ApiParam(name="role",value = "角色id",required = true)@RequestBody RoleVo roleVo){
        System.out.println(roleVo.getId());
        if(roleService.delRole(roleVo.getId())!=0) {
            return R.ok("删除成功！");
        }else {
            return R.error("删除失败，请查看该角色是否是不可删除！");
        }
    }

    @ApiOperation(value="修改Role状态接口",notes="修改角色状态操作接口",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回修改是否成功",examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("role:update")
    @PutMapping("/role/editRoleStatus")
    public R editRoleStatus(@ApiParam(name="role",value = "只需要在role实体类中添加id与status",required = true)@RequestBody RoleVo role){
        System.out.println(role);
        roleService.updateRoleStatus(role.getId(),role.getStatus());
        return R.ok();
    }
}
